package com.msb.api.filter.impl;

import com.msb.api.feign.BeaconCacheClient;
import com.msb.api.filter.CheckFilter;
import com.msb.common.constart.CacheConstant;
import com.msb.common.enums.ExceptionEnums;
import com.msb.common.exception.GraceException;
import com.msb.common.model.StandardSubmit;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author: lmf
 * @Create: 2025/3/7 9:03
 * Description: ip校验
 */
@Slf4j
@Service("ip")
public class IpCheckFilter implements CheckFilter {
    @Resource
    private BeaconCacheClient cacheClient;


    @Override
    public void check(StandardSubmit standardSubmit) {
        String IP_ADDRESS = "ipAddress";

        log.info("校验IpCheckFilter");
        // 查询客户ip白名单
        String ip = cacheClient.hGetToString(CacheConstant.CLIENT_BUSINESS + standardSubmit.getApiKye(), IP_ADDRESS);

        // 白名单为空，或者ip是客户端的ip，则通过校验
        if (StringUtils.isEmpty(ip) || (standardSubmit.getRealIp() != null && ip.contains(standardSubmit.getRealIp()))) {
            log.info("【通过ip】验证后的信息：{}", standardSubmit);
            return;
        }
        // ip不在报名的内容，则返回异常
        GraceException.display(ExceptionEnums.IP_NOT_WHITE);
    }
}
